package com.hqyj.springboot.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class MyAop {
    private final static Logger LOGGER = LoggerFactory.getLogger(MyAop.class);

    //大范围切点
    @Pointcut("execution(public * com.hqyj.springboot.modules.*.controller.*.*(..))")
    @Order(1)
    public void controllerPointcut(){};

    //精确切点
    @Pointcut("@annotation(com.hqyj.springboot.aop.MyAnnotation)")
    @Order(1)
    public void annotationPointcut(){};

    //通知
    @Before(value = "controllerPointcut()")
    public void beforeControllerAop(JoinPoint joinPoint){
    LOGGER.debug("===Before Controller====");
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        LOGGER.debug(String.format("uri: %s",request.getServletPath()));
        LOGGER.debug(String.format("method: %s",request.getMethod()));
        LOGGER.debug(String.format("method: %s",joinPoint.getSignature().getName()));
    }

    @Around(value = "controllerPointcut()")
    public Object aroundControllerAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        LOGGER.debug("===Around Controller====");
        return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }

    @After(value = "controllerPointcut()")
    public void afterControllerAop(JoinPoint joinPoint){
        LOGGER.debug("===After Controller====");
    }

    public Object aroundAnnotationAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
      long start = System.currentTimeMillis();
       Object obj = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
       long end = System.currentTimeMillis();
       LOGGER.debug(String.format("Call method time %s",(end - start)));

       return obj;
    }

}

